package io.qar.controller;

import io.qar.commons.log.annotation.LogOperation;
import io.qar.commons.tools.constant.Constant;
import io.qar.commons.tools.page.PageData;
import io.qar.commons.tools.utils.Result;
import io.qar.commons.tools.validator.AssertUtils;
import io.qar.commons.tools.validator.ValidatorUtils;
import io.qar.commons.tools.validator.group.AddGroup;
import io.qar.commons.tools.validator.group.DefaultGroup;
import io.qar.commons.tools.validator.group.UpdateGroup;

import io.qar.dto.CfgSrcFtpFileDTO;
import io.qar.dto.FtpParamDTO;
import io.qar.service.CfgSrcFtpFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
* 源FTP表
*
* @author yyyyjinying 
* @since 3.0 2023-11-03
*/
@RestController
@RequestMapping("cfgsrcftpfile")
@Api(tags="源FTP表")
public class CfgSrcFtpFileController {
    @Autowired
    private CfgSrcFtpFileService cfgSrcFtpFileService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    // @PreAuthorize("hasAuthority('sys:cfgsrcftpfile:page')")
    public Result<PageData<CfgSrcFtpFileDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<CfgSrcFtpFileDTO> page = cfgSrcFtpFileService.page(params);


        return new Result<PageData<CfgSrcFtpFileDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    // @PreAuthorize("hasAuthority('sys:cfgsrcftpfile:info')")
    public Result<CfgSrcFtpFileDTO> get(@PathVariable("id") Long id){
        CfgSrcFtpFileDTO data = cfgSrcFtpFileService.get(id);

        return new Result<CfgSrcFtpFileDTO>().ok(data);
    }

    /**
     * 全部插入
     * @param ftpParamDTO
     * @return
     */
    @ApiOperation("批量插入")
    @PostMapping("batch/save")
    public Result<Boolean> insertBatchSrcFile(@RequestBody FtpParamDTO ftpParamDTO){
        ValidatorUtils.validateEntity(ftpParamDTO);
        boolean b = cfgSrcFtpFileService.insertBatchSrcFtpFile(ftpParamDTO);
        return new Result<Boolean>().ok(b);
    }

    @ApiOperation("批量更新新增")
    @PostMapping("batch/update")
    public Result<Boolean> updateBatchFromDateTimeFile(@RequestBody FtpParamDTO ftpParamDTO){
        ValidatorUtils.validateEntity(ftpParamDTO);
        boolean b = cfgSrcFtpFileService.updateBatchFromDateTimeFile(ftpParamDTO);
        return new Result<Boolean>().ok(b);
    }





    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    // @PreAuthorize("hasAuthority('sys:cfgsrcftpfile:save')")
    public Result save(@RequestBody CfgSrcFtpFileDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        cfgSrcFtpFileService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    // @PreAuthorize("hasAuthority('sys:cfgsrcftpfile:update')")
    public Result update(@RequestBody CfgSrcFtpFileDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        cfgSrcFtpFileService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    // @PreAuthorize("hasAuthority('sys:cfgsrcftpfile:delete')")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        cfgSrcFtpFileService.delete(ids);

        return new Result();
    }

}